import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class FileTest {
    /**
     * 相对路径的起点(Excel地址)
     */
    private static final String RELATIVE_PATH = "E:\\引绰济辽\\部门工作\\图纸台账-程悦航\\施工蓝图2023.4.29\\";
    public static void main(String[] args) {
        //文件检索路径
        String filePath = "E:\\引绰济辽\\部门工作\\图纸台账-程悦航\\施工蓝图2023.4.29\\1文得根水利枢纽\\3枢纽二标\\1主坝";
        //遍历
        File folder = new File(filePath);
        traverseFolder(folder);
    }

    private static void traverseFolder(File folder) {
        if (folder.isDirectory()) {
            System.out.println("\uD83D\uDFE0文件夹：【" + folder.getName() + "】");

            // 获取文件夹内的文件和子文件夹
            File[] subFiles = folder.listFiles();
            if (subFiles != null) {
                for (File file : subFiles) {
                    // 递归遍历子文件夹
                    traverseFolder(file);
                }
            }
        } else {
            System.out.println("\uD83D\uDD30文件：【" + folder.getName() + "】\n\t\t图号:【" + extractGraphNumber(folder.getName()) + "】\n\t\t" +
                    "相对路径：【" + getRelativePath(folder) + "】");
        }
    }

    private static String getRelativePath(File folder) {
        Path path = Paths.get(RELATIVE_PATH);
        return path.relativize(folder.toPath()).toString();
    }

    public static String extractGraphNumber(String filename) {
        // 定义图号的正则表达式，匹配连续的 '-'、'x'、字母和数字，长度大于等于 10，遇到汉字中断
        Pattern pattern = Pattern.compile("([\\w\\d\\x2D]+)");  // 使用括号进行分组

        Matcher matcher = pattern.matcher(filename);
        String graphNumber = null;

        // 查找匹配的图号
        while (matcher.find()) {
            String match = matcher.group(1);  // 获取第一个分组
            if (match.length() >= 10) {
                graphNumber = match;
                break;  // 找到图号后立即中断
            }
        }

        return graphNumber;
    }
}
